package functions.ems_accountant_job_pool;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.GroupBasicBean;
import com.beans.GroupCrewBean;
import com.crc.mam.BaseCase;

import coms.AutoException;
import coms.Constants;
import frame.HttpHelper;

/**
 * 在岗不在岗的记录
 * @author CHENHAILIAN6
 *
 */
public class AccountantRecord extends BaseCase{
  
  private org.slf4j.Logger logger = LoggerFactory.getLogger(AccountantRecord.class);
  
  /**
   * 查询在岗、不在岗的设置
   * @param settingDiff 0,1 0表示不在岗  ，1表示在岗
   * @param groupName
   * @param groupTypeCode 0,1 0表示核算，1表示复核
   * @return
   */
  public  ArrayList<JSONObject> listOffOrOnDuty(String settingDiff,String groupName,String groupTypeCode) {
    int currentPage = 1;
    int Size = 10;
    if(groupName == null) {
      groupName = "";
    }
    if(groupTypeCode == null) {
      groupTypeCode = "";
    }
    String params = "{\"page\":{\"currentPage\":"+currentPage + ",\"pageSize\":"+Size+",\"recordCount\":0},"
        + "\"params\":{\"groupName\":\""+groupName+"\",\"groupId\":\"\",\"groupTypeCode\":\""+groupTypeCode+"\"},"
        + "\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
    
    String url = Constants.LIST_OFF_DUTY_URL;
    if(settingDiff.equals("1")) {
      url = Constants.LIST_ON_DUTY_URL;
    }
    try {
      result = HttpHelper.post(params, url);
      status = HttpHelper.isOk;
      if(status) {
        logger.info("listOffDuty ---- " + result);
        try {
          JSONObject object = new JSONObject(result);
          int total = object.getInt("total");
          /**
           * 如果小于10条记录，直接存储
           * 如果大于10条记录，循环获取并存放到resultJSONArrayList列表中
           */
          if(total < 10 ) {
             JSONArray array  = object.getJSONArray("list");
             if(array.length() >0) {
               resultJSONArrayList = new  ArrayList<JSONObject>();
               for(int i = 0;i<array.length();i++) {
                 resultJSONArrayList.add(array.getJSONObject(i));
               }
             }
          }else {
            int mode = total % 10;
            int pageSize = total / 10;
            if(mode >0) {
                pageSize  = pageSize +1;
            }
            resultJSONArrayList = new  ArrayList<JSONObject>();
            for(int j=1;j<pageSize+1;j++) {
              params = "{\"page\":{\"currentPage\":"+j + ",\"pageSize\":"+Size+",\"recordCount\":0},"
                  + "\"params\":{\"groupName\":\""+groupName+"\",\"groupId\":\"\",\"groupTypeCode\":\""+groupTypeCode+"\"},"
                  + "\"sortDTO\":{\"fieldName\":\"\",\"orderBy\":\"\"}}";
              result = HttpHelper.post(params, url);
              status = HttpHelper.isOk;
              if(status) {
                logger.info("listOffOrOnDuty ---- " + result);
                try {
                  object = new JSONObject(result);
                  JSONArray array  = object.getJSONArray("list");
                  if(array.length() >0) {
                    resultJSONArrayList = new  ArrayList<JSONObject>();
                    for(int i = 0;i<array.length();i++) {
                      resultJSONArrayList.add(array.getJSONObject(i));
                    }
                  }
                }catch(JSONException e) {
                  
                }
            }
          }
          }
        }catch(JSONException e) {
          
        }
      }else {
        throw new AutoException("查询不在岗信息时出错");
      }
    }catch(Exception e) {
      throw new AutoException("查询不在岗信息时出错");
    }
    return resultJSONArrayList;
  }
  
  /**
   * 保存在岗/不在岗数据  如果组员名称为null 或者为空，表示整个组
   * @param groupType  0,1 0为核算，1为复核
   * @param groupName  通过groupName获取到groupId和groupCode
   * @param crewName   通过组员名获取crewEmpId ，crewId，crewEmpCode 
   * @param recordStime 
   * @param recordEtime
   * @param settingDiff 0,1 0不在岗，1在岗
   */
  public void save(String groupType,String groupName,String crewName,String recordStime,String recordEtime,String settingDiff) {
    Group group = new Group();
    GroupCrewBean crewBean = null;
    GroupBasicBean groupBasicBean = group.findByNameOrType(groupType, groupName);
    if(groupBasicBean == null) {
      throw new AutoException("没有找到对应组名："+groupName+ "的组");
    }else {
      crewBean = group.findCrewByGroupId(groupBasicBean.getGroupId(), crewName);
    }
    if(crewBean == null) {
      crewBean = new GroupCrewBean("","","","","");
    }
    
    String params = "{\"recordId\":\"\",\"recordStime\":\""+recordStime+"\",\"recordEtime\":\""+recordEtime+"\","
        + "\"groupType\":\""+groupType+"\",\"groupId\":"+groupBasicBean.getGroupId()+",\"groupCode\":\""+groupBasicBean.getGroupCode()+"\","
            + "\"crewEmpId\":"+crewBean.getCrewEmpId()+",\"crewId\":"+crewBean.getCrewId()+","
        + "\"crewEmpCode\":\""+crewBean.getCrewEmpCode()+"\",\"settingDiff\":\""+settingDiff+"\"}";
    String url = Constants.ACCOUNTANT_RECORD_SAVE_URL;
    try {
      result = HttpHelper.post(params, url);
      status = HttpHelper.isOk;
      if(status) {
        try {
          JSONObject object = new JSONObject(result);
          if(object.get("recordId") != null) {
            
          }else {
            logger.error("保存在岗/不在岗时出错了，保存成功应该有recordId值");
            throw new AutoException("保存在岗/不在岗时出错了，保存成功应该有recordId值");
          }
        }catch(JSONException e) {
          throw new AutoException("JSON解析错误");
        }
      }else {
        logger.error("返回结果不为200");
      }
    }catch(Exception e) {
      throw new AutoException("保存在岗不在岗时服务器异常");
    }
    
    
   }
}
